package dao.Impl;

import dao.BaseDao;
import dao.ShopCartDao;
import model.entity.Dish;
import model.entity.ShoppingCart;
import util.NowTime;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ShopCartDaoImpl extends BaseDao implements ShopCartDao {

    @Override
    public List<ShoppingCart> get(int id) {
        List<ShoppingCart> carts=new ArrayList<>();
        String sql="select * from shopping_cart where user_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                ShoppingCart s=new ShoppingCart();
                s.setId(rs.getInt("id"));
                s.setUser_id(rs.getInt("user_id"));
                s.setName(rs.getString("name"));
                s.setNumber(rs.getInt("number"));
                s.setImage(rs.getString("image"));
                s.setAmount(rs.getFloat("amount"));
                s.setCreate_time(String.valueOf(rs.getTimestamp("create_time")));
                s.setDish_id(rs.getInt("dish_id"));
                s.setSetmeal_id(rs.getInt("setmeal_id"));
                s.setCategory_id(rs.getInt("category_id"));
                carts.add(s);
            }
            System.out.println("查看购物车成功!");
        }
        catch (SQLException e){
            System.out.println("查看购物车失败!"+e);
        }
        return carts;
    }

    @Override
    public void deleteById(int id) {
        String sql="delete from `shopping_cart` where id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeUpdate();
            System.out.println("删除购物车商品成功!");
        }
        catch (SQLException e){
            System.out.println("删除购物车商品失败!"+e);
        }

    }

    @Override
    public void add(String name, String image, int user_id, int dish_id, int setmeal_id, int number, float amount) {
        String sql="insert into shopping_cart (name, image, user_id, dish_id, setmeal_id, amount, create_time) value (?,?,?,?,?,?,?);";
        try{
            NowTime time=new NowTime();
            String t=time.getTime();
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2,image);
            pstmt.setInt(3,user_id);
            pstmt.setInt(4,dish_id);
            pstmt.setInt(5,setmeal_id);
            pstmt.setFloat(6,amount);
            pstmt.setTimestamp(7, Timestamp.valueOf(t));
            pstmt.executeUpdate();
            System.out.println("购物车添加商品成功!");
        }
        catch (Exception e){
            System.out.println("购物车添加商品失败!"+e);
        }
    }

    @Override
    public Dish find(int id) {
        Dish d=new Dish();
        String sql="select * from dish where id=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                d.setId(rs.getInt("id"));
                d.setImage(rs.getString("image"));
                d.setName(rs.getString("name"));
                d.setPrice(rs.getFloat("price"));
                d.setDescription(rs.getString("description"));
                d.setCategory_id(rs.getInt("category_id"));
                d.setCreate_time(String.valueOf(rs.getTimestamp("create_time")));
                d.setCreate_user(rs.getInt("create_user"));
                d.setUpdate_user(rs.getInt("update_user"));
            }
            System.out.println("按id查寻菜品成功!");
        }
        catch (SQLException e){
            System.out.println("按id查寻菜品失败!");
        }
        return d;
    }

    @Override
    public List<Integer> getId(int dishId) {
        List<Integer> list=new ArrayList<>();
        String sql="select distinct dish_id from shopping_cart;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                list.add(rs.getInt("dish_id"));
            }
            System.out.println("查询购物车所有菜品id成功!");
        }
        catch (SQLException e){
            System.out.println("查询购物车所有菜品id失败!"+e);
        }
        return list;
    }

    @Override
    public int getNum(int dishId) {
        int num = 1;
        String sql="select number from shopping_cart where dish_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,dishId);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                num =rs.getInt("number");
            }
            System.out.println("按id获取购物车菜品数量成功!");
        }
        catch (SQLException e){
            System.out.println("按id获取购物车菜品数量失败!"+e);
        }
        return num;
    }

    @Override
    public void addOne(int dishId) {
        String sql="update shopping_cart set number=number+1 where dish_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,dishId);
            pstmt.executeUpdate();
            System.out.println("商品数量+1成功!");
        }
        catch (SQLException e){
            System.out.println("商品数量+1失败!"+e);
        }
    }

    @Override
    public void upAmount(int dishId,float amount) {
        String sql="update shopping_cart set amount=? where dish_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setFloat(1,amount);
            pstmt.setInt(2,dishId);
            pstmt.executeUpdate();
            System.out.println("购物车单个菜品总价修改成功!");
        }
        catch (SQLException e){
            System.out.println("购物车单个菜品总价修改失败!"+e);
        }
    }

    @Override
    public void delAll(int userId) {
        String sql="delete from shopping_cart where user_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userId);
            pstmt.executeUpdate();
            System.out.println("清空购物车成功!");
        }
        catch (SQLException e){
            System.out.println("清空购物车失败!"+e);
        }
    }

    @Override
    public float getAmount(int userId) {
        float amount=0;
        String sql="select sum(amount) from shopping_cart where user_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userId);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                amount=rs.getFloat("`sum(amount)`");
            }
            System.out.println("获取购物车总价成功!");
        }
        catch (SQLException e){
            System.out.println("获取购物车总价失败!"+e);
        }
        return amount;
    }
    @Override
    public List<ShoppingCart> Get(int userId) {
        List<ShoppingCart> list=new ArrayList<>();
        String sql="select  * from shopping_cart where user_id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userId);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                ShoppingCart s=new ShoppingCart();
                s.setId(rs.getInt("id"));
                s.setImage(rs.getString("image"));
                s.setAmount(rs.getFloat("amount"));
                s.setNumber(rs.getInt("number"));
                s.setName(rs.getString("name"));
                s.setDish_id(rs.getInt("dish_id"));
                s.setSetmeal_id(rs.getInt("setmeal_id"));
                s.setCategory_id(rs.getInt("category_id"));
                s.setCreate_time(String.valueOf(rs.getTimestamp("create_time")));
                s.setUser_id(rs.getInt("user_id"));
                list.add(s);
            }
            System.out.println("按用户id查询购物车成功!");
        }
        catch (SQLException e){
            System.out.println("按用户id查询购物车失败!"+e);
        }
        return list;
    }

    @Override
    public List<ShoppingCart> list(int userId, Integer dishId, Integer setmealId) {
        List<ShoppingCart> shoppingCarts=new ArrayList<>();
        String sql="select * from `shopping_cart` where user_id=?";
        if(dishId!=null){
            sql+=" and dish_id="+dishId;
        }else{
            sql+=" and setmeal_id="+setmealId;
        }
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userId);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while (rs.next()){
                ShoppingCart shoppingCart = new ShoppingCart();
                shoppingCart.setId(rs.getInt("id"));
                shoppingCart.setName(rs.getString("name"));
                shoppingCart.setAmount(rs.getFloat("amount"));
                shoppingCart.setNumber(rs.getInt("number"));
                shoppingCart.setImage(rs.getString("image"));
                shoppingCart.setDish_id(rs.getInt("dish_id"));
                shoppingCart.setSetmeal_id(rs.getInt("setmeal_id"));
                shoppingCart.setCreate_time(String.valueOf(rs.getTime("create_time")));
                shoppingCart.setUser_id(rs.getInt("user_id"));
                shoppingCarts.add(shoppingCart);
            }
        } catch (SQLException e) {
            System.out.println("查询购物车中是否存在商品错误："+e);
        }
        return shoppingCarts;
    }

    @Override
    public void updateNumberById(ShoppingCart shoppingCart) {
        String sql="update `shopping_cart` set number=? where id=?;";
        try {
            pstmt= conn.prepareStatement(sql);
            pstmt.setInt(1,shoppingCart.getNumber());
            pstmt.setInt(2,shoppingCart.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("修改购物车中商品数量失败："+e);
        }
    }

    @Override
    public void insert(ShoppingCart shoppingCart) {
        String sql="insert into `shopping_cart` (name,image,user_id,dish_id,setmeal_id,category_id,number,amount,create_time) values (?,?,?,?,?,?,?,?,?);";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,shoppingCart.getName());
            pstmt.setString(2, shoppingCart.getImage());
            pstmt.setInt(3,shoppingCart.getUser_id());
            pstmt.setInt(4,shoppingCart.getDish_id());
            pstmt.setInt(5,shoppingCart.getSetmeal_id());
            pstmt.setInt(6,shoppingCart.getCategory_id());
            pstmt.setInt(7,shoppingCart.getNumber());
            pstmt.setDouble(8,shoppingCart.getAmount());
            pstmt.setString(9,shoppingCart.getCreate_time());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("新增购物车错误："+e);
        }
    }
}
